package cn.com.chengmandian.bike.controller;


import cn.com.chengmandian.agent.utils.AuthUtil;
import cn.com.chengmandian.bike.service.IEbRedeemCodeService;
import cn.com.chengmandian.core.web.pojo.AjaxResult;
import cn.com.chengmandian.core.web.utils.LocalMapUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 电单车运营-兑换码 前端控制器
 * </p>
 *
 * @author AutoGenerator
 * @since 2023-10-18
 */
@Api(tags = "兑换码")
@RestController
@RequestMapping("/redeem/code")
public class EbRedeemCodeController {

    @Autowired
    private IEbRedeemCodeService redeemCodeService;

    @ApiOperation("兑换")
    @PostMapping("/use/{code}")
    public AjaxResult redeem(@PathVariable("code") String code){
        Integer areaId = LocalMapUtil.getAgentInfo().getAreaId();
        Assert.notNull(areaId, "缺少区域ID");

        Integer memberId = AuthUtil.getAgentUser().getId();
        // 效验兑换码是否可用
        redeemCodeService.checkRedeemCode(code, areaId, memberId);

        // 使用兑换码
        redeemCodeService.useRedeemCode(code, memberId);
        return AjaxResult.success();
    }
}
